home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: news3.noc.netcom.net!zdc!zippo!usenet
- From: txwang@public.sta.net.cn (Wang)
- Subject: Re: Heap Memory Management
- X-Newsreader: Forte Free Agent 1.0.82
- Sender: usenet@news.zippo.com
- Nntp-Posting-Host: ts1-15.sta.net.cn
- Organization: No Organization
- Message-ID: <DM9z8L.4Ep@news.zippo.com>
- References: <1996Feb4.140550.4699@wisipc.weizmann.ac.il>
- Date: Sun, 4 Feb 1996 23:42:45 GMT
-
- Kajdan Dimitry <cerlpvk> wrote:
-
- | Hi.
- | Would someone explain please.
-
- | Suppose one got a function that allocates memory on the free store.
-
- | char* func(int n)
- | {
- | char* a=new char[n];
- | strcpy(a,"Hello");
- | return a;
- | }
-
- | int main()
- | {
-
- | char* b=func(10);
-
- | return(0);
-
- | }
-
- | The question is how the memory allocated by new can be freed, since as soon as
- | func() terminates the user loses access to it.
- | Thanks a lot
-
- No, you haven't lost access to it. You assigned the pointer to b,
- right? So,
-
- int main()
- {
- char *b = func(10);
- delete b;
- return 0;
- }
-
- will do what you want.
-
- Have fun.
-
- --
- Wang TianXing
-
-
-